﻿using Common.Class;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Extra.ERP;
using Security.Layout;
using Security.Security;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Security.ERP
{
    public class frmChangePassword : XtraForm
    {
        private SimpleButton btClose;
        private SimpleButton btLogin;
        private IContainer components = null;
        private CONTROL_FormTableAdapter controL_FormTableAdapter;
        private dsLayout dsLayout;
        private Label lblConfirmpassword;
        private Label lblCurrentpassword;
        private Label lblNewpassword;
        private Label lblTitle;
        private PictureEdit picLogin;
        private int solanDangNhap = 0;
        private TextBox txtLapLaiMatKhauMoi;
        private TextBox txtMatKhauCu;
        private TextBox txtMatKhauMoi;

        public frmChangePassword()
        {
            this.InitializeComponent();
            SYS_LOG.Insert("Thay Đổi Mật Khẩu", "Xem");
            this.Text = (SysOption.Language == "VN") ? this.Text : "Change password";
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            SYS_LOG.Insert("Thay Đổi Mật Khẩu", "Cập Nhật", MyLogin.UserId);
            string password = MyLogin.CreatePassword(MyLogin.Account, this.txtMatKhauCu.Text);
            SYS_USER sys_user = new SYS_USER();
            sys_user.GetUserName(MyLogin.Account);
            if (password.CompareTo(sys_user.Password) != 0)
            {
                XtraMessageBox.Show("Bạn nhập mật khẩu hiện tại kh\x00f4ng đ\x00fang!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtMatKhauCu.Text = "";
                this.txtMatKhauCu.Focus();
                this.solanDangNhap++;
                if (this.solanDangNhap >= 3)
                {
                    XtraMessageBox.Show("Bạn đ\x00e3 nhập sai qu\x00e1 số lần cho ph\x00e9p!\n\nPhần mềm sẽ tự động tắt!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    base.Close();
                    Application.ExitThread();
                }
            }
            else if (this.txtMatKhauMoi.Text.CompareTo(this.txtLapLaiMatKhauMoi.Text) != 0)
            {
                XtraMessageBox.Show("Mật khẩu nhập lại kh\x00f4ng giống nhau! Xin vui l\x00f2ng nhập lại!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtMatKhauMoi.Text = "";
                this.txtLapLaiMatKhauMoi.Text = "";
                this.txtMatKhauMoi.Focus();
                this.solanDangNhap++;
                if (this.solanDangNhap >= 3)
                {
                    XtraMessageBox.Show("Bạn đ\x00e3 nhập sai qu\x00e1 số lần cho ph\x00e9p!\n\nPhần mềm sẽ tự động tắt!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    base.Close();
                    Environment.Exit(0);
                }
            }
            else
            {
                password = MyLogin.CreatePassword(MyLogin.Account, this.txtMatKhauMoi.Text);
                if (sys_user.Update(sys_user.UserID, sys_user.UserName, password, sys_user.Group_ID, sys_user.Email, "", "", sys_user.Management, sys_user.BranchCode, sys_user.DepartmentCode, sys_user.GroupCode, sys_user.EmployeeCode, true) == "OK")
                {
                    XtraMessageBox.Show("Bạn đ\x00e3 thay đổi mật khẩu th\x00e0nh c\x00f4ng!", "Ho\x00e0n Th\x00e0nh", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
                else
                {
                    XtraMessageBox.Show("Bạn chưa thay đổi được mật khẩu mới!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(frmChangePassword));
            this.txtMatKhauMoi = new TextBox();
            this.lblNewpassword = new Label();
            this.txtMatKhauCu = new TextBox();
            this.lblCurrentpassword = new Label();
            this.txtLapLaiMatKhauMoi = new TextBox();
            this.lblConfirmpassword = new Label();
            this.lblTitle = new Label();
            this.dsLayout = new dsLayout();
            this.controL_FormTableAdapter = new CONTROL_FormTableAdapter();
            this.picLogin = new PictureEdit();
            this.btLogin = new SimpleButton();
            this.btClose = new SimpleButton();
            this.dsLayout.BeginInit();
            this.picLogin.Properties.BeginInit();
            base.SuspendLayout();
            this.txtMatKhauMoi.BackColor = SystemColors.ControlLightLight;
            this.txtMatKhauMoi.Font = new Font("Tahoma", 9.75f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.txtMatKhauMoi.ForeColor = Color.Navy;
            this.txtMatKhauMoi.Location = new Point(0x70, 0x4b);
            this.txtMatKhauMoi.Name = "txtMatKhauMoi";
            this.txtMatKhauMoi.PasswordChar = '*';
            this.txtMatKhauMoi.Size = new Size(0xe5, 0x17);
            this.txtMatKhauMoi.TabIndex = 1;
            this.txtMatKhauMoi.UseSystemPasswordChar = true;
            this.txtMatKhauMoi.KeyPress += new KeyPressEventHandler(this.txtMatKhauMoi_KeyPress);
            this.lblNewpassword.AutoSize = true;
            this.lblNewpassword.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblNewpassword.Location = new Point(6, 0x4c);
            this.lblNewpassword.Name = "lblNewpassword";
            this.lblNewpassword.Size = new Size(0x47, 13);
            this.lblNewpassword.TabIndex = 0x41;
            this.lblNewpassword.Text = "Mật Khẩu Mới";
            this.txtMatKhauCu.BackColor = SystemColors.ControlLightLight;
            this.txtMatKhauCu.Font = new Font("Tahoma", 9.75f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.txtMatKhauCu.ForeColor = Color.Navy;
            this.txtMatKhauCu.Location = new Point(0x70, 0x2b);
            this.txtMatKhauCu.Name = "txtMatKhauCu";
            this.txtMatKhauCu.PasswordChar = '*';
            this.txtMatKhauCu.Size = new Size(0xe5, 0x17);
            this.txtMatKhauCu.TabIndex = 0;
            this.txtMatKhauCu.UseSystemPasswordChar = true;
            this.txtMatKhauCu.KeyPress += new KeyPressEventHandler(this.txtMatKhauCu_KeyPress);
            this.lblCurrentpassword.AutoSize = true;
            this.lblCurrentpassword.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblCurrentpassword.Location = new Point(6, 0x2d);
            this.lblCurrentpassword.Name = "lblCurrentpassword";
            this.lblCurrentpassword.Size = new Size(0x44, 13);
            this.lblCurrentpassword.TabIndex = 0x3f;
            this.lblCurrentpassword.Text = "Mật Khẩu Cũ";
            this.txtLapLaiMatKhauMoi.BackColor = SystemColors.ControlLightLight;
            this.txtLapLaiMatKhauMoi.Font = new Font("Tahoma", 9.75f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.txtLapLaiMatKhauMoi.ForeColor = Color.Navy;
            this.txtLapLaiMatKhauMoi.Location = new Point(0x70, 0x6a);
            this.txtLapLaiMatKhauMoi.Name = "txtLapLaiMatKhauMoi";
            this.txtLapLaiMatKhauMoi.PasswordChar = '*';
            this.txtLapLaiMatKhauMoi.Size = new Size(0xe5, 0x17);
            this.txtLapLaiMatKhauMoi.TabIndex = 2;
            this.txtLapLaiMatKhauMoi.UseSystemPasswordChar = true;
            this.txtLapLaiMatKhauMoi.KeyPress += new KeyPressEventHandler(this.txtLapLaiMatKhauMoi_KeyPress);
            this.lblConfirmpassword.AutoSize = true;
            this.lblConfirmpassword.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblConfirmpassword.Location = new Point(6, 110);
            this.lblConfirmpassword.Name = "lblConfirmpassword";
            this.lblConfirmpassword.Size = new Size(0x60, 13);
            this.lblConfirmpassword.TabIndex = 0x43;
            this.lblConfirmpassword.Text = "Nhập Lại Mật Khẩu";
            this.lblTitle.AutoSize = true;
            this.lblTitle.Font = new Font("Tahoma", 9.75f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblTitle.Location = new Point(0x7b, 11);
            this.lblTitle.Name = "lblTitle";
            this.lblTitle.Size = new Size(0x8e, 0x10);
            this.lblTitle.TabIndex = 0x45;
            this.lblTitle.Text = "THAY ĐỔI MẬT KHẨU";
            this.dsLayout.DataSetName = "dsLayout";
            this.dsLayout.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
            this.controL_FormTableAdapter.ClearBeforeFill = true;
            this.picLogin.EditValue = manager.GetObject("picLogin.EditValue");
            this.picLogin.Location = new Point(0x1a, 5);
            this.picLogin.Name = "picLogin";
            this.picLogin.Properties.AllowFocused = false;
            this.picLogin.Properties.Appearance.BackColor = Color.Transparent;
            this.picLogin.Properties.Appearance.ForeColor = Color.Transparent;
            this.picLogin.Properties.Appearance.Options.UseBackColor = true;
            this.picLogin.Properties.Appearance.Options.UseForeColor = true;
            this.picLogin.Properties.BorderStyle = BorderStyles.NoBorder;
            this.picLogin.Properties.PictureStoreMode = PictureStoreMode.Image;
            this.picLogin.Properties.ReadOnly = true;
            this.picLogin.Properties.ShowMenu = false;
            this.picLogin.Properties.SizeMode = PictureSizeMode.Stretch;
            this.picLogin.Size = new Size(0x20, 0x20);
            this.picLogin.TabIndex = 0x48;
            this.btLogin.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btLogin.Appearance.Font = new Font("Tahoma", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btLogin.Appearance.ForeColor = Color.FromArgb(0, 0, 0xc0);
            this.btLogin.Appearance.Options.UseFont = true;
            this.btLogin.Appearance.Options.UseForeColor = true;
            this.btLogin.Image = (Image)manager.GetObject("btLogin.Image");
            this.btLogin.Location = new Point(0x70, 0x8f);
            this.btLogin.Name = "btLogin";
            this.btLogin.Size = new Size(70, 0x1b);
            this.btLogin.TabIndex = 3;
            this.btLogin.Text = "Đồng \x00fd";
            this.btLogin.Click += new EventHandler(this.btLuu_Click);
            this.btClose.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btClose.Appearance.Font = new Font("Tahoma", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btClose.Appearance.Options.UseFont = true;
            this.btClose.Image = (Image)manager.GetObject("btClose.Image");
            this.btClose.Location = new Point(0xd1, 0x8f);
            this.btClose.Name = "btClose";
            this.btClose.Size = new Size(70, 0x1b);
            this.btClose.TabIndex = 4;
            this.btClose.Text = "&Tho\x00e1t";
            this.btClose.Click += new EventHandler(this.btThoat_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.AutoSize = true;
            base.ClientSize = new Size(0x162, 0xb6);
            base.Controls.Add(this.btLogin);
            base.Controls.Add(this.btClose);
            base.Controls.Add(this.picLogin);
            base.Controls.Add(this.lblTitle);
            base.Controls.Add(this.txtLapLaiMatKhauMoi);
            base.Controls.Add(this.lblConfirmpassword);
            base.Controls.Add(this.txtMatKhauMoi);
            base.Controls.Add(this.lblNewpassword);
            base.Controls.Add(this.txtMatKhauCu);
            base.Controls.Add(this.lblCurrentpassword);
            this.DoubleBuffered = true;
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "frmChangePassword";
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Thay Đổi Mật Khẩu";
            this.dsLayout.EndInit();
            this.picLogin.Properties.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys keys = keyData;
            if (keys != Keys.Escape)
            {
                if (keys != Keys.F10)
                {
                    return false;
                }
            }
            else
            {
                if (base.ParentForm != null)
                {
                    base.ParentForm.Close();
                }
                return true;
            }
            if (base.ParentForm != null)
            {
                base.ParentForm.Close();
            }
            return true;
        }

        private void txtLapLaiMatKhauMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btLuu_Click(sender, e);
            }
        }

        private void txtMatKhauCu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.txtMatKhauMoi.Focus();
            }
        }

        private void txtMatKhauMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.txtLapLaiMatKhauMoi.Focus();
            }
        }
    }
}